//
//  MYGMedicalToolsInputView.swift
//  MYGrow
//
//  Created by 林矗 on 2024/3/10.
//

import UIKit

enum MYGMedicalToolsInputViewType {
    case age
    case weight
    case height
    case custom(title: String, unit: String, placeholder: String, keyboardType: UIKeyboardType)
    var title: String {
        switch self {
        case .age:
            return "年龄"
        case .weight:
            return "体重"
        case .height:
            return "身高"
        case .custom(let title, _, _, _):
            return title
        }
    }
    
    var unit: String {
        switch self {
        case .age:
            return "岁"
        case .weight:
            return "kg"
        case .height:
            return "cm"
        case .custom(_, let unit, _, _):
            return unit
        }
    }
    
    var placeholder: String {
        switch self {
        case .age:
            return "请输入年龄"
        case .weight:
            return "请输入体重"
        case .height:
            return "请输入身高"
        case .custom(_, _, let placeholder, _):
            return placeholder
        }
    }
    
    var keyboardType: UIKeyboardType {
        switch self {
        case .age:
            return UIKeyboardType.numberPad
        case .weight:
            return UIKeyboardType.decimalPad
        case .height:
            return UIKeyboardType.decimalPad
        case .custom(_, _, _, let keyboardType):
            return keyboardType
        }
    }
}

class MYGMedicalToolsInputView: UIView {
    // MARK: - Property
    private var type: MYGMedicalToolsInputViewType
    var inputDidChange:((String) -> ())?
    // MARK: - init
    init(type: MYGMedicalToolsInputViewType, frame: CGRect) {
        self.type = type
        super.init(frame: frame)
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - UI
    private func configUI() {
        addSubviews([
            titleLabel,
            textFiled,
            unitLabel
        ])
        
        titleLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(0)
        }
        
        unitLabel.snp.makeConstraints { make in
            make.right.equalTo(0)
            make.centerY.equalToSuperview()
            make.width.greaterThanOrEqualTo(22.5)
        }
        
        textFiled.snp.makeConstraints { make in
            make.right.equalTo(unitLabel.snp.left).offset(-10)
            make.centerY.equalToSuperview()
            make.width.equalTo(160)
        }
        
        titleLabel.text = type.title
        textFiled.keyboardType = type.keyboardType
        textFiled.placeholder = type.placeholder
        textFiled.setPlaceHolderTextColor(.rgba(204, 204, 204))
        unitLabel.text = type.unit
    }
    
    // MARK: - Public funcs
    
    // MARK: - lazy load
    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(16)
        label.textColor = .rgba(51, 51, 51)
        return label
    }()
    
    private lazy var textFiled: UITextField = {
        let tf = UITextField()
        tf.textAlignment = .right
        tf.textColor = .rgba(51, 51, 51)
        tf.font = .regular(16)
        tf.addTarget(self, action: #selector(textFiledEditingDidChange), for: .editingChanged)
        return tf
    }()
    
    private lazy var unitLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(16)
        label.textColor = .rgba(84, 84, 84)
        label.textAlignment = .right
        return label
    }()
}

extension MYGMedicalToolsInputView {
    @objc func textFiledEditingDidChange() {
        inputDidChange?(textFiled.text ?? "")
    }
}
